﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using BOHVote.BLL;

namespace BOH.ControlPanel
{
    public partial class UnitModify : System.Web.UI.Page
    {
        protected string PageType = string.Empty;
        protected string ID = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            PageType = Request.QueryString["type"];
            ID = Request.QueryString["ID"];
            if (!IsPostBack)
            {
                BindGovern();
                if (PageType != null && ID != null && PageType == "edit" && ID.Trim().Length != 0)
                {
                    FillForm(ID);
                }
            }
        }

        public void FillForm(string ID)
        {
            DataSet dst = UnitClass.getUnitByID(ID);
            if (dst.Tables[0].Rows.Count > 0)
            {
                this.txtAddress.Text = dst.Tables[0].Rows[0]["address"].ToString();
                this.txtContactPerson.Text = dst.Tables[0].Rows[0]["ContactPerson"].ToString();
                this.txtContactPhone.Text = dst.Tables[0].Rows[0]["ContactPhone"].ToString();
                this.txtMaxID.Text = dst.Tables[0].Rows[0]["MaxID"].ToString();
                this.txtUnitID.Text = dst.Tables[0].Rows[0]["UnitID"].ToString();
                this.txtUnitName.Text = dst.Tables[0].Rows[0]["UnitName"].ToString();
                this.txtPostalCode.Text = dst.Tables[0].Rows[0]["PostalCode"].ToString();
                this.drpG.SelectedValue = dst.Tables[0].Rows[0]["GovernID"].ToString();
                this.drpLevel.SelectedValue = dst.Tables[0].Rows[0]["HospitalLevel"].ToString();
                this.drpStatus.SelectedValue = dst.Tables[0].Rows[0]["Status"].ToString();
            }
        }


        public void BindGovern()
        {
            DataSet dstGovern = UnitClass.getGovern();
            drpG.DataSource = dstGovern.Tables[0].DefaultView;
            drpG.DataTextField = "GovernName";
            drpG.DataValueField = "GovernID";
            drpG.DataBind();
            ListItem listitem = new ListItem("", "");
            listitem.Selected = true;
            drpG.Items.Add(listitem);
        }

        protected void butSave_Click(object sender, EventArgs e)
        {
            if (PageType != null && ID != null && PageType == "edit" && ID.Trim().Length != 0)
            {
                UnitClass.UpdateUnit(ID, this.txtUnitID.Text, this.drpG.SelectedValue, this.txtUnitName.Text, this.txtAddress.Text, this.txtPostalCode.Text, this.txtContactPerson.Text, this.txtContactPhone.Text, this.drpLevel.SelectedValue, this.txtMaxID.Text, this.drpStatus.SelectedValue);
            }
            else
            {
                UnitClass.InserUnit(this.txtUnitID.Text, this.drpG.SelectedValue, this.txtUnitName.Text, this.txtAddress.Text, this.txtPostalCode.Text, this.txtContactPerson.Text, this.txtContactPhone.Text, this.drpLevel.SelectedValue, this.txtMaxID.Text, this.drpStatus.SelectedValue);
            }
        }
    }
}
